# -*- coding: UTF-8 -*-
# 描述数据买家（任务发布者）
BUDGET = 50  # 预算


class Buyer:
    def __init__(self, id, budget=-1):
        self.id = id  # index
        # 预算
        if(budget < 0):
            self.budget = BUDGET
        else:
            self.budget = budget
        self.w = []  # 发来报价的卖家

    # 获取总服务质量
    def getUtility(self, s):
        u = 0
        for i in self.w:
            u += s[i].quality[self.id]
        return u

    # 获取总费用
    def getCost(self, s):
        c = 0
        for i in self.w:
            c += s[i].price[self.id]
        return c

    # 从报价单中选择卖家，DP算法
    # s:卖家集合
    def chooseSeller(self, s):
        # 清空之前的选择
        path = [[0 for c in range(self.budget + 1)]
                for r in range(len(self.w))]
        # 初始化dp数组
        dp = [0 for col in range(self.budget + 1)]
        w = self.w
        # i:卖家
        for i in range(len(w)):
            # j:预算
            for j in range(self.budget, s[w[i]].price[self.id]-1, -1):
                nadd = dp[j]
                add = dp[j - s[w[i]].price[self.id]] + s[w[i]].quality[self.id]
                # 若在该处选择了添加i，将path置1
                if (add > nadd):
                    path[i][j] = 1
                dp[j] = max(add, nadd)
        # 找出选择了哪些，更新w
        choice = []
        i = len(self.w) - 1
        j = self.budget
        while (i >= 0 and j > 0):
            if (path[i][j] == 1):
                choice.append(w[i])
                j -= s[w[i]].price[self.id]
            i -= 1
        self.w = choice
        return
